<template>
  <div class="title">Promise 语法</div>
  <div class="inner">一种异步解决方案</div>
</template>

<script setup>

let numFlag=1;
console.log(1);
//promise 语法
const promiseResult2=new Promise(function (resolve) {
   setTimeout(()=>{
    console.log(2);
    resolve('resolve')
   },2000)
}).then((data)=>{
    console.log(3);
    console.log(data);
    return 'return'
}).then((data2)=>{console.log(data2);})

//promise 函数封装
const promiseResult1=promiseFunDemo().then((data)=>{console.log(data);});
function promiseFunDemo(){
    if(numFlag===1){
    const promiseResult=new Promise(function (resolve) {
       setTimeout(()=>{
       console.log(2);
       resolve('resolve')
       },4000)});
    return promiseResult;
  }else if(numFlag==2){
    return Promise.resolve('转换成promise对象');
  }else{
    return Promise.resolve(100);
  }
}
</script>

<style>

</style>